package com.zyx.spring5msb.aoptx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/30 10:12
 */
@Repository
public class BookDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 减去某个用户的余额
     */
    public void updateBalance(String userName, int balance) {
        String sql = "update account set balance=balance-? where username=?";
        jdbcTemplate.update(sql, balance, userName);
    }

    /**
     * 按照图书的id来获取图书的价格
     */
    public Integer getPrice(int id) {
        String sql = "select price from book where id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, id);
    }

    /**
     * 减库存, 减去某本书的库存
     */
    public void updateStock(int id) {
        String sql = "update book_stock set stock=stock-1 where id=?";
        jdbcTemplate.update(sql, id);
    }

    /**
     * 修改图书价格
     */
    public void updatePrice(int id, int price) {
        String sql = "update book set price=? where id =?";
        jdbcTemplate.update(sql, price, id);
    }

}
